package com.magicsoft.weitown.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.magicsoft.app.helper.DialogHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 用于显示Loading时继承BaseFragment类
 * 
 * @author liqingjun
 * 
 */
public abstract class BaseFragment extends Fragment {

	private Dialog loading;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
	}

	public void showLoading(String msg) {

		if (loading == null) {
			loading = DialogHelper.createLoadingDialog(getActivity(), msg);

			loading.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					if (keyCode == KeyEvent.KEYCODE_BACK
							&& event.getRepeatCount() == 0) {

						// TODO:按返回键取消loading事件
						getActivity().onBackPressed();
					}
					return false;
				}
			});

		}
		loading.show();

	}

	public void hideLoading() {

		if (loading != null) {

			if (loading.isShowing()) {
				loading.dismiss();
				loading = null;
			}

		}
	}

	public Boolean isShowingLoading() {

		if (loading != null) {
			return loading.isShowing();
		}

		return false;

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onDestroy() {
		hideLoading();
		super.onDestroy();
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

}
